package com.grom.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.dispacher.context.ContextHolder;
import com.grom.po.StoreInfo;
import com.grom.service.StoreInfoService;
import com.grom.util.JsonUtil;
import com.grom.util.SecurityUtil;
import com.holder.log.ConsoleLog;

public class StoreInfoAction {
	public void gotoList() {
		List<StoreInfo> allStoreInfos = StoreInfoService.instance
				.getAllStoreInfo();
		ContextHolder.setRequestAttribute("allStoreInfos", allStoreInfos);
		for (StoreInfo storeInfo : allStoreInfos) {
			ConsoleLog.debug(JsonUtil.parseToJson(storeInfo));
		}
		ContextHolder.forward("/WEB-INF/jsp/storeInfo/update.jsp");
	}

	public void doSave() {
		StoreInfo storeInfo = (StoreInfo) ContextHolder
				.initFromTheRequest(StoreInfo.class);
		if (storeInfo.getID() == null || storeInfo.getID().trim().equals("")) {
			StoreInfoService.instance.add(storeInfo);
		} else {
			StoreInfoService.instance.update(storeInfo);
			;
		}
		ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri()
				+ "/StoreInfoAction/gotoList.do");
	}

	public void getDetail() {
		String id = ContextHolder.findInstance().getRequest()
				.getParameter("itemid");
		StoreInfo storeInfo = StoreInfoService.instance.getStoreInfoById(id);
		String json = JsonUtil.parseToJson(storeInfo);
		HttpServletResponse response = ContextHolder.findInstance()
				.getResponse();
		try {
			PrintWriter out = response.getWriter();
			out.write("storeInfo=" + json);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void doDelete() {
		String id = ContextHolder.findInstance().getRequest()
				.getParameter("deleteId");
		System.out.println("| StoreInfoAction | doDelete | id | " + id);
		StoreInfo storeInfo = StoreInfoService.instance.getStoreInfoById(id);
		if (!SecurityUtil.isUserHasRight(storeInfo.getUserId())) {
			ContextHolder.sendRedirect(ContextHolder.findInstance()
					.getContextUri() + "/StoreInfoAction/gotoList.do");
			return;
		}
		storeInfo.setID(id);
		StoreInfoService.instance.delete(storeInfo);
		ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri()
				+ "/StoreInfoAction/gotoList.do");
	}

	public void conditionSelect() {
		String shopOverviewId_s = ContextHolder.findInstance().getRequest()
				.getParameter("shopOverviewId_s");
		String codeOfABDStore_s = ContextHolder.findInstance().getRequest()
				.getParameter("codeOfABDStore_s");
		String sql = "select * from storeinfo where shopOverviewId like '%"
				+ shopOverviewId_s + "%' and codeOfABDStore like '%"
				+ codeOfABDStore_s + "%'";
		List<StoreInfo> allStoreInfos = StoreInfoService.instance
				.conditionSelect(sql);
		ContextHolder.setRequestAttribute("allStoreInfos", allStoreInfos);
		ContextHolder.forward("/WEB-INF/jsp/storeInfo/update.jsp");
	}
}
